package com.ty.listener;

import com.ty.dto.InventoryChangSource;
import com.ty.enums.InventoryChangType;
import com.ty.event.InventoryChangEvent;
import com.ty.service.InventoryService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;

public class InventoryListener implements ApplicationListener<InventoryChangEvent>, ApplicationContextAware {


    private ApplicationContext applicationContext;

    @Override
    public void onApplicationEvent(InventoryChangEvent event) {


        // source 触发该事件的源
        InventoryChangSource source = (InventoryChangSource) event.getSource();
        // bean  策略模式的上下文类
        InventoryService bean = applicationContext.getBean(InventoryService.class);
        bean.option(source);
        /**
        if (source.getType().getCode() ==  InventoryChangType.IN.getCode()){
            //获取到事件源 处理入库
        } else if (source.getType().getCode() == InventoryChangType.SALE_OUT.getCode()) {
            //获取到事件源  处理销售出库
        }
         *
         */

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
